package com.fastjrun.springcloud.simple.controller;

import com.fastjrun.springcloud.simple.service.FromService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 服务接口
 */
@RestController
public class HelloController {
    @Autowired
    FromService fromService;
    /*注入“服务提供者”的名称*/
    @Value("${provider.name}")
    private String name;
    /*注入“服务提供者”的端口号*/
    @Value("${server.port}")
    private String port;

    /*提供的接口，用于返回信息*/
    @RequestMapping("/hello")
    public String hello() {
        String ipAddr = "";
        String hostName = "";
        try {
            InetAddress inetAddress = InetAddress.getLocalHost();
            byte[] addr = inetAddress.getAddress();
            for (int i = 0; i < addr.length; i++) {
                if (i > 0) {
                    ipAddr += ".";
                }
                ipAddr += addr[i] & 0xFF;
            }
            hostName = inetAddress.getHostName();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        String str =
          "ipAddr:" + ipAddr + ";hostName:" + hostName + ";provider:" + name + ";port:" + port;
        //返回数据
        return str;
    }

    @RequestMapping("/from")
    public String from() {
        return fromService.from();
    }
}
